package com.cgg.stock.common.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * Mybatis-plus的配置类
 */

@SpringBootConfiguration
public class MPConfig {

    //配置一个分页插件
    //MybatisPlusInterceptor: mybatis-plus拦截器栈（链）
    @Bean
    public MybatisPlusInterceptor createMybatisPlusInterceptor(){
        //创建一个拦截器链
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();

        //创建一个分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();

        //设置拦截器对象相关属性

        //往拦截器栈添加分页拦截器
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }


    //乐观锁插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

}
